<?php 
/**
 * QFrameWork
 *
 * @author: quyang
 * @date: 2010-1-17
 * @version: 1.0
 */

/**
 * Core of Framework, do something essential.
 */
class Core {
	
	static public function getSingleton($file,$class){
		if (!is_object($GLOBALS[CORE_SHARE_OBJECTS][$class]) && is_file($file)) {
			require_once($file);
			$GLOBALS[CORE_SHARE_OBJECTS][$class] = new $class;
		}
		return $GLOBALS[CORE_SHARE_OBJECTS][$class];
	}
    
    /**
     * 加载数据库操作类
     *
     * @param string $type
     * @return object / false
     */
    static public function db($type = 'mysql') {
        //缓存池中的KEY
        $_name = 'DB_'.$type;
        if (Core::_is_set($_name))
            return Core::_get($_name);
            
        $classname = 'lib_db_'.$type;
        $db = new $classname;
        
        Core::_set($_name, $db);
        
        return $db;
    }
    /**
     * 加载MC操作类
     *
     * @param string $type
     * @return object
     */
	static public function mc($type = '') {
        //缓存池中的KEY
        $_name = 'MC_'.$type;
        if (Core::_is_set($_name))
            return Core::_get($_name);

        $MC_CONFIG = Core::config('mc_config');
        if (!isset($MC_CONFIG[$type]))
            return false;
            
        $mc = new lib_memcached($MC_CONFIG[$type]);
        Core::_set($_name, $mc);
        
        return $mc;
    }
    /**
     * CACHE控制类实例
     *
     * @return object
     */
    static public function cache() {
        //缓存池中的KEY
        $_name = 'MC_CACHE';
        if (Core::_is_set($_name))
            return Core::_get($_name);
            
        $obj = new mc_cache();
        
        Core::_set($_name, $obj);
        return $obj;
    }
    
    /**
     * 生成微秒级时间
     *
     * @return int
     */
    static public function microtime() {
        return array_sum(explode(' ', microtime()));
    }
    /**
     * 普通HASH算法　主要用来HASH数据库和表
     *
     * @param mix $key
     * @param int $hash_n
     * @return int
     */
    static public function hash_table($u, $n = 128) {
        $h = sprintf("%u", crc32($u));
        $h1 = intval($h / $n);
        $h2 = $h1 % $n;
        $h3 = base_convert($h2, 10, 16);
        $h4 = sprintf("%02s", $h3);
        return $h4;
    }
    /**
     * 哈希分库
     *
     * @param int $uid
     * @param int $hash_n
     * @return int
     */
    static public function hash_db($uid, $hash_n = 4) {
        $h = sprintf("%u", crc32($uid));
        $index = intval(fmod($h, $hash_n));
        return $index;
    }
    
    static public function http_get(&$result, $host, $url, $port = 80, $referer = CORE_DOMAIN , $timeout = CORE_HTTP_TIMEOUT ) {
        $url = 'http://'.trim($host, '/').':'.$port.$url;
        
        $ch_curl = curl_init();
        curl_setopt($ch_curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch_curl, CURLOPT_HEADER, false);
        curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);
        
        curl_setopt($ch_curl, CURLOPT_REFERER, $referer); //
        curl_setopt($ch_curl, CURLOPT_URL, $url); //地址
        curl_setopt($ch_curl, CURLOPT_HTTPGET, 1); //
        
        $iStartTime = Core::microtime();
        //重复执行2次
        $result = curl_exec($ch_curl);
        if ($result === false) {
            $iStartTime = Core::microtime();
            $result = curl_exec($ch_curl);
        }
        if ($result === false) {
            $log = 'url:'.$url.TAB.'e:'.curl_error($ch_curl).TAB.'p:'.$port.TAB.'r:'.$referer.TAB.'t:'.$timeout;
            Logger::debug('http_get', $log, 'error');
            Logger::alert(CORE_LOG_ALERT_API, $host, 'connect_err');
        }
        
        if (CORE_DEBUG) {
            $iLastTime = Core::microtime() - $iStartTime;
            $log = 'url:'.$url.TAB.'rs:'.$result.TAB.'t:'.$iLastTime;
            Logger::debug('http_get', $log);
        }
        
        return $result === false ? false : true;
    }
    
    static public function http_post(&$result, $host, $url, $aData, $port = 80, $referer = CORE_DOMAIN , $timeout = CORE_HTTP_TIMEOUT ) {
        $url = 'http://'.trim($host, '/').':'.$port.$url;
        
        $ch_curl = curl_init();
        curl_setopt($ch_curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch_curl, CURLOPT_HEADER, false);
        curl_setopt($ch_curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch_curl, CURLOPT_REFERER, $referer); //referer
        curl_setopt($ch_curl, CURLOPT_URL, $url); //调用地址
        curl_setopt($ch_curl, CURLOPT_POST, 1); //
        curl_setopt($ch_curl, CURLOPT_POSTFIELDS, $aData); //数据
        
        $iStartTime = Core::microtime();
        $result = curl_exec($ch_curl);
        if ($result === false) {
            $iStartTime = Core::microtime();
            $result = curl_exec($ch_curl);
            
            $log = 'url:'.$url.' e:'.curl_error($ch_curl).' p:'.$port.' r:'.$referer.' t:'.$timeout;
            Logger::debug('http_get', $log, 'error');
            Logger::alert(CORE_LOG_ALERT_API, $host, 'connect_err');
        }
        
        if (CORE_DEBUG) {
            $iLastTime = Core::microtime() - $iStartTime;
            $log = 'url:'.$url.TAB.'rs:'.$result.TAB.'t:'.$iLastTime.TAB.'data:'.serialize($aData);
            Logger::debug('http_get', $log);
        }
        
        return $result === false ? false : true;
    }
    
    /**
     * 系统错误强行退出
     *
     * @param string $log  需要记录的日志
     */
    static public function halt($log) {
        Logger::debug('halt', CORE_LOG_SIGN_ERROR.$log.$_SERVER['REQUEST_URI']);
        die();
    }
    /**
     * 生成SESSION对象
     *
     * @return object
     */
    static public function session() {
        //TODO
    }
    /**
     * 检测当前COOKIE是否登录
     *
     * @param array $info
     * @return bool true已登录/false未登录
     */
    static public function is_login(&$info) {
        //TODO
    }
}
?>
